﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class ChildView : Form
    {
        Viewer ChildViewer;
        public bool[] Display;
        public Bitmap original;

        public ChildView(bool[] Display)
        {
            InitializeComponent();
            ChildViewer=new Viewer();
            ChildViewer.initializeViewer(1,0,Controller.MyMap.Height);
            Transformer.Reset(ChildViewer);
            this.Display = Display;
            DrawAll(Display);
        }

        private void DrawAll(bool[] Display)
        {
            Bitmap newBitmap = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics g = Graphics.FromImage(newBitmap);
            g.Clear(Color.White);
            for (int j = Controller.MyMap.Base_Order.Count - 1; j >= 0; j--)
            {
                if (Display[Controller.MyMap.Layer_List.Count + j] == true)
                {
                    g.DrawImage(Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[j] - 1].DrawChange(), 0, 0);
                }
            }
            for (int i = Controller.MyMap.Layer_Order.Count() - 1; i >= 0; i--)
            {
                
                if (Display[i] == true)
                {
                    if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CPointLayer)
                    {
                        g.DrawImage(((CPointLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpoint(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CLineLayer)
                    {
                        g.DrawImage(((CLineLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawlinelayer(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CPolygonLayer)
                    {
                        g.DrawImage(((CPolygonLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpolygon(), 0, 0);
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CTextLayer)
                    {
                        g.DrawImage(((CTextLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawtext(), 0, 0);
                    }
                }
            }
            pictureBox.Image = newBitmap;
        }
        enum OperationStyle { ZoomIn, ZoomOut, Panning,None };
        OperationStyle OpStyle=OperationStyle.None;
        Point start_Point = new Point();
        Point end_Point = new Point();
        bool Select_position = false;
        private void _ZoomIn_Click(object sender, EventArgs e)
        {
            OpStyle = OperationStyle.ZoomIn;
            pictureBox.Cursor = MainForm.pMainWin.ZoominCursor;
        }

        private void _ZoomOut_Click(object sender, EventArgs e)
        {
            OpStyle = OperationStyle.ZoomOut;
            pictureBox.Cursor = MainForm.pMainWin.ZoominCursor;
        }

        private void _ZoomToLayer_Click(object sender, EventArgs e)
        {
            ChildViewer.ZoomToLayer();
            DrawAll(Display);
            pictureBox.Cursor = Cursors.Arrow;
        }

        private void _Pan_Click(object sender, EventArgs e)
        {
            OpStyle = OperationStyle.Panning;
            pictureBox.Cursor = Cursors.NoMove2D;
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if(OpStyle==OperationStyle.ZoomIn)
            {
                start_Point.X = e.X;
                start_Point.Y = e.Y;
                Select_position = true;
                original = new Bitmap(pictureBox.Image);
            }
            else if (OpStyle == OperationStyle.ZoomOut)
            {
                start_Point.X = e.X;
                start_Point.Y = e.Y;
                Select_position = true;
            }
            else if(OpStyle==OperationStyle.Panning)
            {
                start_Point.X = e.X;
                start_Point.Y = e.Y;
                Select_position = true;
            }
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (OpStyle == OperationStyle.ZoomIn)
            {
                Transformer.Reset(ChildViewer);
                end_Point.X = e.X;
                end_Point.Y = e.Y;
                Select_position = false;
                //拉框放大
                if (Math.Abs(end_Point.X - start_Point.X) > 2 || Math.Abs(end_Point.Y - start_Point.Y) > 2)
                {
                    ChildViewer.ZoomIn_Multi(start_Point,end_Point);
                    DrawAll(Display);
                }
                //单点放大
                else
                {
                    ChildViewer.ZoomIn_Point(end_Point);
                    DrawAll(Display);
                }
            }
            else if (OpStyle == OperationStyle.ZoomOut)
            {
                Transformer.Reset(ChildViewer);
                end_Point.X = e.X;
                end_Point.Y = e.Y;
                Select_position = false;
                ChildViewer.ZoomOut(end_Point);
                DrawAll(Display);
            }
            else if (OpStyle == OperationStyle.Panning)
            {
                Transformer.Reset(ChildViewer);
                end_Point.X = e.X;
                end_Point.Y = e.Y;
                Select_position = false;
                ChildViewer.Pan(start_Point,end_Point);
                DrawAll(Display);
            }
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (OpStyle == OperationStyle.ZoomIn&&Select_position==true)
            {
                Bitmap Temp=new Bitmap(original);
                Graphics temp=Graphics.FromImage(Temp);
                Pen blackPen = new Pen(Color.Black, 2f);
                temp.DrawRectangle(blackPen, start_Point.X, start_Point.Y, (e.X - start_Point.X), (e.Y - start_Point.Y));
                pictureBox.Image = Temp;
                temp.Dispose();
            }
        }
    }
}
